﻿using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Responses;
using Infrastructure.ExternalService.Core.Selfregister;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;

namespace Infrastructure.ExternalService.Core.Imp.SelfRegister
{
    public class SelfregisterClient : BaseHttpClient, ISelfregisterClient
    {
        private readonly OrderCenterApiOptions _orderingOptions;

        public SelfregisterClient(IOptions<OrderCenterApiOptions> options
      , HttpClient client
      , ILoggerFactory loggerFactory)
      : base(client, loggerFactory, options.Value.SelfRegisterOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(SelfregisterClient)}配置获取失败");

            loggerFactory.CreateLogger<SelfregisterClient>();
        }

        /// <summary>
        /// 自主续学获取资料收集上传url
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<DataGatherResponse>> GetDataGatherAsync(GetDataGatherRequest request)
        {
            var response = await PostAsync<WrapResponse<List<DataGatherResponse>>>(
            _orderingOptions.SelfRegisterOptions.GetUrlByProSpecIds, request);

            if (!response.Code.Equals(ClientReturnCode.Success))
            {
                throw new HttpProxyException($"{response.Msg}");
            }

            if (response.Data != null && response.Data.Count >= 0)
            {
                return response.Data;
            }

            throw new UserFriendlyException($"{response.Msg}");
        }
    }
}
